package cn.crm.Service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.crm.Service.IPermissionService;
import cn.crm.common.ServerResponse;
import cn.crm.entity.Permission;
import cn.crm.mapper.PermissionMapper;
import cn.crm.vo.LayuiMVO;

@Service
public class PermissionServiceImpl implements IPermissionService{

	@Autowired
	private PermissionMapper permissionmapper;
	
	@Override
	public ServerResponse selectAllpermissions() {
		// TODO Auto-generated method stub
		List<Permission> list = permissionmapper.pageList(new Permission());
		List<LayuiMVO> layuiMVOs = new ArrayList<>();
		for (Permission permission : list) {
			LayuiMVO layuiMVO = new LayuiMVO();
			layuiMVO.setId(permission.getId());
			layuiMVO.setName(permission.getName());
			layuiMVO.setStatus(1);
			layuiMVOs.add(layuiMVO);
		}
		return ServerResponse.createSuccess("查找成功", layuiMVOs);
	}

	@Override
	public ServerResponse pageList(Integer page, Integer limit, Permission permission) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, limit);
		List list = permissionmapper.pageList(permission);
		PageInfo pageInfo = new PageInfo<>(list);
		int count = (int) pageInfo.getTotal();
		return ServerResponse.createSuccess("成功", count, list);
	}

	@Override
	public ServerResponse insert(Permission permission) {
		int count = permissionmapper.insert(permission);
		if (count == 1) {
			return ServerResponse.createSuccess("添加成功");
		}else {
			return ServerResponse.createError("添加失败");
		}
	}


}
